/*********************************************************************************
 *      Copyright:  (C) 2022 zengwen<1825984037@qq.com>
 *                  All rights reserved.
 *
 *       Filename:  database.c
 *    Description:  This file 
 *                 
 *        Version:  1.0.0(07/07/22)
 *         Author:  zengwen <1825984037@qq.com>
 *      ChangeLog:  1, Release initial version on "07/07/22 15:04:23"
 *                 
 ********************************************************************************/
#include <stdio.h>
#include <string.h>

#include "logger.h"
#include "sqlite3.h"
#include "database.h"

//打开数据库

int open_data_sqlite (sqlite3 **db)
{
	int         ret;
	char       *errmsg = 0;

	ret=sqlite3_open("RPI.db",db);//打开当前路径下的RPI.db数据库标识
	if(ret!=SQLITE_OK)
	{
		log_err("Failed to open database!:%s\n",sqlite3_errmsg(*db));
		return -1;
	}
	else
	{
		log_info("Open database sucessfuly!\n");
	}

	char  *sql="CREATE TABLE IF NOT EXISTS sendata(\
				ID INTEGER PRIMARY KEY AUTOINCREMENT,\
				SN       TEXT,\
				TM       TEXT,\
				TEMP     TEXT\
				);";
	ret=sqlite3_exec(*db,sql,NULL,NULL,&errmsg);
	if(ret != SQLITE_OK)
	{
		log_err("Create table error:%s\n",errmsg);
		sqlite3_free(errmsg);
		return -2;
	}
	log_info("Table create successfully\n");
	return 0;
}

//保存数据到数据库

int save_data_sqlite(sqlite3 *db,char Data[])
{
	char                *SN;
	char                *TM;
	char                *TEMP;
	char                 sql_in[256];
	int                  rv=0;
	char                *errmsg=0;
	const char           s[2] = "/";

	SN=strtok(Data, s);
	TM=strtok(NULL,s);
	TEMP=strtok(NULL,s);
	sprintf(sql_in, "INSERT INTO sendata(SN,TM,TEMP) VALUES('%s', '%s','%s');", SN,TM,TEMP);//构造sqlite语句
	rv=sqlite3_exec(db,sql_in,NULL,NULL,&errmsg);

	if (rv == SQLITE_OK)
	{
		log_info("Insert data OK! rv=%d\n", rv);
	}
	else
	{
		log_err("Insert data Error!%s\n", strerror(errno));
		sqlite3_free(errmsg);
	}

	return 0;
}

//获取数据库保存的数据

int get_data_sqlite(sqlite3 *db,char Buf[])
{
	int           rv=-1;
	char         *sql="SELECT * FROM sendata LIMIT 1;";
	const char   *error=NULL;
	sqlite3_stmt *pstmt;
	int           nCol = 1;
	char         *sn;
	char         *time;
	char         *temp;

	rv = sqlite3_prepare_v2(db, sql, strlen(sql), &pstmt, &error);
	if( rv < 0 )
	{
		log_err("get tables failue:%s\n",strerror(errno));
	}
	if(sqlite3_step( pstmt ) == SQLITE_ROW)
	{
		sn= (char *)sqlite3_column_text(pstmt,1);
		temp= (char *)sqlite3_column_text(pstmt,2);
		time=(char *)sqlite3_column_text(pstmt,3);
	}
	snprintf(Buf,strlen(time)*6,"%s/%s/%s",sn,temp,time);

	return 0;
}

//删除数据库已发送的数据

int delete_data_sqlite(sqlite3 *db)
{
	int   rv = -1;
	char  *error = NULL;
	char  *sql = "delete from  sendata where rowid in (select rowid from sendata LIMIT 1);";
	rv = sqlite3_exec(db, sql, NULL, NULL, &error);

	if( rv < 0)
	{
		printf("delete data form table failure:%s\n",strerror(errno));
		sqlite3_free(error);
		return -1;
	}
	log_info("delece a record.\n");
	return 0;
}
